import { EyeOutlined, SearchOutlined } from '@ant-design/icons';
import {
  Button,
  Card,
  Col,
  DatePicker,
  Form,
  Input,
  Row,
  Select,
  Space,
  Table,
  message,
} from 'antd';
import type { ColumnsType } from 'antd/es/table';
import React from 'react';
import './index.less';

const WorkAnalysis: React.FC = () => {
  const [form] = Form.useForm();

  // 处理查看详情
  const handleViewDetails = (record: any) => {
    message.info(`查看律师 ${record.leadLawyer} 的工作详情`);
  };

  // 处理快捷选择
  const handleQuickSelect = (type: string) => {
    const today = new Date();
    let startDate: Date;
    let endDate: Date = today;

    switch (type) {
      case 'thisYear':
        startDate = new Date(today.getFullYear(), 0, 1);
        break;
      case 'last3to6Months':
        startDate = new Date(today.getFullYear(), today.getMonth() - 6, 1);
        endDate = new Date(today.getFullYear(), today.getMonth() - 3, 0);
        break;
      case 'last3Months':
        startDate = new Date(today.getFullYear(), today.getMonth() - 3, 1);
        break;
      case 'lastMonth':
        startDate = new Date(today.getFullYear(), today.getMonth() - 1, 1);
        endDate = new Date(today.getFullYear(), today.getMonth(), 0);
        break;
      case 'thisWeek': {
        const dayOfWeek = today.getDay();
        startDate = new Date(today.getTime() - (dayOfWeek - 1) * 24 * 60 * 60 * 1000);
        break;
      }
      default:
        return;
    }

    form.setFieldsValue({
      dateRange: [startDate, endDate],
    });
  };

  // 处理查询
  const handleSearch = () => {
    const values = form.getFieldsValue();
    console.log('查询条件:', values);
    message.success('查询成功');
  };

  // 处理重置
  const handleReset = () => {
    form.resetFields();
    message.info('已重置筛选条件');
  };

  // 表格列定义
  const columns: ColumnsType<any> = [
    {
      title: '序号',
      dataIndex: 'index',
      key: 'index',
      width: 80,
      align: 'center',
    },
    {
      title: '主办律师',
      dataIndex: 'leadLawyer',
      key: 'leadLawyer',
      width: 120,
    },
    {
      title: '办案总量',
      dataIndex: 'totalCases',
      key: 'totalCases',
      width: 120,
      align: 'center',
    },
    {
      title: '已结案件',
      dataIndex: 'closedCases',
      key: 'closedCases',
      width: 120,
      align: 'center',
    },
    {
      title: '在办案件',
      dataIndex: 'ongoingCases',
      key: 'ongoingCases',
      width: 120,
      align: 'center',
    },
    {
      title: '诉前案件',
      dataIndex: 'pretrialCases',
      key: 'pretrialCases',
      width: 120,
      align: 'center',
    },
    {
      title: '诉讼案件',
      dataIndex: 'litigationCases',
      key: 'litigationCases',
      width: 120,
      align: 'center',
    },
    {
      title: '已判未执行案件',
      dataIndex: 'judgedUnexecutedCases',
      key: 'judgedUnexecutedCases',
      width: 150,
      align: 'center',
    },
    {
      title: '执行案件',
      dataIndex: 'executionCases',
      key: 'executionCases',
      width: 120,
      align: 'center',
    },
    {
      title: '委托终止案件',
      dataIndex: 'terminatedCases',
      key: 'terminatedCases',
      width: 150,
      align: 'center',
    },
    {
      title: '在办案件标的总额',
      dataIndex: 'totalAmount',
      key: 'totalAmount',
      width: 180,
      align: 'center',
    },
    {
      title: '已到账基础律师费',
      dataIndex: 'basicFeesReceived',
      key: 'basicFeesReceived',
      width: 180,
      align: 'center',
    },
    {
      title: '已到账风险律师费',
      dataIndex: 'riskFeesReceived',
      key: 'riskFeesReceived',
      width: 180,
      align: 'center',
    },
    {
      title: '已缴纳保证金',
      dataIndex: 'depositPaid',
      key: 'depositPaid',
      width: 150,
      align: 'center',
    },
    {
      title: '已退回保证金',
      dataIndex: 'depositRefunded',
      key: 'depositRefunded',
      width: 150,
      align: 'center',
    },
    {
      title: '已缴纳违约金',
      dataIndex: 'penaltyPaid',
      key: 'penaltyPaid',
      width: 150,
      align: 'center',
    },
    {
      title: '操作',
      key: 'action',
      width: 100,
      fixed: 'right',
      render: (_, record) => (
        <Button
          type="link"
          size="small"
          icon={<EyeOutlined />}
          onClick={() => handleViewDetails(record)}
        >
          查看
        </Button>
      ),
    },
  ];

  // 模拟数据
  const mockData = [
    {
      key: '1',
      index: 1,
      leadLawyer: '张三',
      totalCases: '1000件',
      closedCases: '500件',
      ongoingCases: '500件',
      pretrialCases: '50件',
      litigationCases: '60件',
      judgedUnexecutedCases: '140件',
      executionCases: '150件',
      terminatedCases: '0件',
      totalAmount: '2500万',
      basicFeesReceived: '800万',
      riskFeesReceived: '600万',
      depositPaid: '60万',
      depositRefunded: '60万',
      penaltyPaid: '0元',
    },
    {
      key: '2',
      index: 2,
      leadLawyer: '李勤',
      totalCases: '800件',
      closedCases: '600件',
      ongoingCases: '200件',
      pretrialCases: '30件',
      litigationCases: '240件',
      judgedUnexecutedCases: '60件',
      executionCases: '170件',
      terminatedCases: '1件',
      totalAmount: '2800万',
      basicFeesReceived: '700万',
      riskFeesReceived: '600万',
      depositPaid: '60万',
      depositRefunded: '60万',
      penaltyPaid: '0元',
    },
    {
      key: '3',
      index: 3,
      leadLawyer: '李四',
      totalCases: '700件',
      closedCases: '450件',
      ongoingCases: '250件',
      pretrialCases: '30件',
      litigationCases: '20件',
      judgedUnexecutedCases: '120件',
      executionCases: '200件',
      terminatedCases: '2件',
      totalAmount: '2300万',
      basicFeesReceived: '900万',
      riskFeesReceived: '800万',
      depositPaid: '70万',
      depositRefunded: '70万',
      penaltyPaid: '0元',
    },
    {
      key: '4',
      index: 4,
      leadLawyer: '王五',
      totalCases: '800件',
      closedCases: '300件',
      ongoingCases: '500件',
      pretrialCases: '40件',
      litigationCases: '10件',
      judgedUnexecutedCases: '40件',
      executionCases: '40件',
      terminatedCases: '1件',
      totalAmount: '1000万',
      basicFeesReceived: '600万',
      riskFeesReceived: '600万',
      depositPaid: '60万',
      depositRefunded: '60万',
      penaltyPaid: '0元',
    },
    {
      key: '5',
      index: 5,
      leadLawyer: '王强',
      totalCases: '600件',
      closedCases: '200件',
      ongoingCases: '400件',
      pretrialCases: '20件',
      litigationCases: '60件',
      judgedUnexecutedCases: '10件',
      executionCases: '20件',
      terminatedCases: '3件',
      totalAmount: '800万',
      basicFeesReceived: '800万',
      riskFeesReceived: '700万',
      depositPaid: '70万',
      depositRefunded: '70万',
      penaltyPaid: '0元',
    },
  ];

  return (
    <div className="work-analysis">
      <Card>
        <Form form={form} layout="vertical">
          <Row gutter={16} align="bottom">
            <Col span={6}>
              <Form.Item label="请输入主办律师姓名">
                <Input placeholder="请输入主办律师姓名" suffix={<SearchOutlined />} />
              </Form.Item>
            </Col>
            <Col span={4}>
              <Form.Item label="主办律师:">
                <Select placeholder="选择主办律师">
                  <Select.Option value="张三">张三</Select.Option>
                  <Select.Option value="李勤">李勤</Select.Option>
                  <Select.Option value="李四">李四</Select.Option>
                  <Select.Option value="王五">王五</Select.Option>
                  <Select.Option value="王强">王强</Select.Option>
                </Select>
              </Form.Item>
            </Col>
            <Col span={6}>
              <Form.Item label="日期:">
                <DatePicker.RangePicker
                  style={{ width: '100%' }}
                  placeholder={['开始日期', '截止日期']}
                />
              </Form.Item>
            </Col>
            <Col span={8}>
              <Form.Item label="快捷选择:">
                <Space wrap>
                  <Button size="small" onClick={() => handleQuickSelect('thisYear')}>
                    今年内
                  </Button>
                  <Button size="small" onClick={() => handleQuickSelect('last3to6Months')}>
                    近三六月内
                  </Button>
                  <Button size="small" onClick={() => handleQuickSelect('last3Months')}>
                    近三个月内
                  </Button>
                  <Button size="small" onClick={() => handleQuickSelect('lastMonth')}>
                    近一个月内
                  </Button>
                  <Button size="small" onClick={() => handleQuickSelect('thisWeek')}>
                    本周
                  </Button>
                </Space>
              </Form.Item>
            </Col>
          </Row>

          <div className="form-buttons">
            <Button type="primary" onClick={handleSearch}>
              查询
            </Button>
            <Button onClick={handleReset} style={{ marginLeft: 8 }}>
              重置
            </Button>
          </div>
        </Form>

        <Table
          columns={columns}
          dataSource={mockData}
          scroll={{ x: 'max-content' }}
          pagination={{
            total: 20,
            pageSize: 5,
            showSizeChanger: true,
            showQuickJumper: true,
            showTotal: (total) => `共${total}条`,
            current: 2,
          }}
        />
      </Card>
    </div>
  );
};

export default WorkAnalysis;
